package com.zenithmind.article.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zenithmind.article.pojo.domain.ArticleTag;
import com.zenithmind.article.pojo.domain.Tag;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;


@Mapper
public interface ArticleTagMapper extends BaseMapper<ArticleTag> {

    //连表查询文章标签关联表-文章表-标签表,最后返回标签信息
    @Select("select t.* from article a join article_tag at on a.id=at.article_id join tag t on at.tag_id=t.id where a.id=#{articleId}")
    List<Tag> selectTagsArticleTag(String articleId);
    
    /**
     * 获取标签使用统计
     * @param limit 返回数量限制
     * @return 标签使用统计列表
     */
    @Select("SELECT t.id, t.name, COUNT(at.id) as count " +
            "FROM tag t " +
            "JOIN article_tag at ON t.id = at.tag_id " +
            "GROUP BY t.id " +
            "ORDER BY count DESC " +
            "LIMIT #{limit}")
    List<Map<String, Object>> getTagUsageStats(Integer limit);
}
